package com.maple.date;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class Calendar extends GregorianCalendar{
	 private static final long serialVersionUID = -3035083924420107566L;
	 private String[] months={"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
     private String[] weeks={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; 
     public String getMonth()
	 {
		return months[this.get(MONTH)];
	 }
     public String getDayOfWeek()
	 {
		return weeks[this.get(DAY_OF_WEEK)];
	 }
     
     public String toStringOfChina()
     {
    	 return ( this.get(Calendar.YEAR)+"-"+
    	    	 (this.get(Calendar.MONTH)+1)+"-"+
    	    	 this.get(Calendar.DAY_OF_MONTH)+" "+
    	    	 this.get(Calendar.HOUR_OF_DAY)+":"+
    	    	 this.get(Calendar.MINUTE)+":"+
    	    	 this.get(Calendar.SECOND));
     }
     
 	public static final Integer DAY_BEFORE=0;
	public static final Integer DAY_AFTER=1;
	
	public static Map<Integer,Date> obtainBeforeAndAfterDay(Date date)
	{
		Map<Integer,Date>  map=new HashMap<Integer,Date>();
		GregorianCalendar calendarBefore=new GregorianCalendar();
		calendarBefore.setTime(date);
		calendarBefore.add(Calendar.DATE, -1);
		calendarBefore.set(Calendar.HOUR_OF_DAY, 23);
		calendarBefore.set(Calendar.MINUTE, 59);
		calendarBefore.set(Calendar.SECOND, 59);
		Date dateBefore=new Date(calendarBefore.getTimeInMillis());
		map.put(DAY_BEFORE,dateBefore);
		
		
		GregorianCalendar calendarAfter=new GregorianCalendar();
		calendarAfter.setTime(date);
		calendarAfter.add(Calendar.DATE, 1);
		calendarAfter.set(Calendar.HOUR_OF_DAY, 0);
		calendarAfter.set(Calendar.MINUTE, 0);
		calendarAfter.set(Calendar.SECOND, 0);
		Date dateAfter=new Date(calendarAfter.getTimeInMillis());
		map.put(DAY_AFTER,dateAfter);
		return map;
	}
	
 	public static final Integer MONTH_BEFORE=0;
	public static final Integer MONTH_AFTER=1;
	public static Map<Integer,Date> obtainBeforeAndAfterMonth(int year,int month)
	{
		
		Calendar c=new Calendar();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month-1);
		
		
		Map<Integer,Date>  map=new HashMap<Integer,Date>();
		Calendar calendarBefore=(Calendar) c.clone();
		calendarBefore.set(Calendar.DAY_OF_MONTH, 1);
		calendarBefore.add(Calendar.DAY_OF_MONTH, -1);
		calendarBefore.set(Calendar.HOUR_OF_DAY, 23);
		calendarBefore.set(Calendar.MINUTE, 59);
		calendarBefore.set(Calendar.SECOND, 59);
		Date dateBefore=new Date(calendarBefore.getTimeInMillis());
		map.put(MONTH_BEFORE,dateBefore);
		
		
		Calendar calendarAfter=(Calendar) c.clone();
		calendarAfter.set(Calendar.DAY_OF_MONTH, 1);
		calendarAfter.add(Calendar.MONTH, 1);
		calendarAfter.set(Calendar.HOUR_OF_DAY, 0);
		calendarAfter.set(Calendar.MINUTE, 0);
		calendarAfter.set(Calendar.SECOND, 0);
		Date dateAfter=new Date(calendarAfter.getTimeInMillis());
		map.put(MONTH_AFTER,dateAfter);
		return map;
	}
	
	@Test
	public void testobject() throws Exception {
		 Map<Integer,Date>  map=Calendar.obtainBeforeAndAfterMonth(2012, 11);
		 Date db=map.get(Calendar.MONTH_BEFORE);
		 Date da=map.get(Calendar.MONTH_AFTER);
		 System.out.println(db.toLocaleString());
		 System.out.println(da.toLocaleString());
	}
}
